import json
import datetime

import scrapy
from robot.appcan import AppItem


class SjqqSpider(scrapy.Spider):
    name = 'sjqq'

    start_urls = [
        'http://sj.qq.com/myapp/cate/appList.htm?orgame=1&categoryId=0&pageSize=20&pageContext=0']
    #start_urls = ['http://www.hetushu.com/book/2/index.html']

    def parse(self, response):
        k = json.loads(response.text)
        if not k['success'] or not k['obj']:          
            self.logger.info('%s success is false',k)
            return
        for item in k['obj']:
            pa = item['apkPublishTime']
            publish_at = datetime.datetime.fromtimestamp(pa)            
            yield AppItem({
                'site':self.name,
                'id':item['appId'],
                'name': item['appName'],
                'file_size':item['fileSize'],
                'download_cnt':item['appDownCount'],
                'icon_url':item['iconUrl'],
                'new_feature':item['newFeature'],
                'pkg_name':item['pkgName'],
                'version_code':item['versionCode'],
                'version_name':item['versionName'],
                'images':item['images'],
                'publish_at':publish_at.isoformat(),
                'author':{
                    'id':item['authorId'],
                    'name':item['authorName']
                },
                'category':{
                    'id':item['categoryId'],
                    'name':item['categoryName'],
                },                
            })
        self.logger.info(k['pageContext'])
        yield response.follow('http://sj.qq.com/myapp/cate/appList.htm?orgame=1&categoryId=0&pageSize=20&pageContext=%s' % (k['pageContext'],), self.parse)
